package itsm.isperp.module.controller.performance;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.performance.PerPerformanceItem;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.performance.PerPerformanceItemService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-08-26 10:40
 */
@Controller
@RequestMapping("performance/performanceItem")
public class PerPerformanceItemController extends
    BaseController<PerPerformanceItem> {

  @Autowired
  protected PerPerformanceItemService perPerformanceItemService;

  @Override
  public PerPerformanceItemService getService() {
    return this.perPerformanceItemService;
  }

  @Override
  public String getPageTitle() {
    return "考核指标";
  }

  @RequestMapping("edit")
  @Override
  public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();
    PerPerformanceItem model = null;
    if (StringUtils.isEmpty(id)) {
      model = new PerPerformanceItem();
      model.setId(HibernateUUIDGenerator.generate());
      model.setComputeType("人工输入");
    } else {
      model = this.getService().findOne(id);
    }
    String sourceId = req.getParameter("sourceId");
    if (StringUtils.isNotBlank(sourceId))
      model.setPerSettingId(sourceId);

    AppDictService appDictService = ContextHolder
        .getSpringBean("appDictService");
    List<AppDict> computeType = appDictService.findByType("PER_COMPUTE_TYPE");
    mv.addObject("computeType", computeType);
    mv.addObject("model", model);
    return mv;
  }

  @RequestMapping("listDetail")
  @Override
  @ResponseBody
  public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
      HttpServletRequest req) {
    String sourceId=req.getParameter("sourceId");
    if(StringUtils.isNotBlank(sourceId))
       request.addFilterRule("per_setting_id",JqGridFilterRuleOp.eq, sourceId);
    return this.getService().findAllDetailByRequest(request).toPage();

  }

}
